//
//  BaseViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//

#import "BaseViewController.h"
#import "Singleton.h"
#import "AFNetworkReachabilityManager.h"

@interface BaseViewController ()

@property(nonatomic, copy)NSString *currentStatu;

@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    if ([Singleton shareSingleton].isNight) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDayModel) name:@"NightDayNSNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDayModel) name:@"LightDayNSNotification" object:nil];
    
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
                [Singleton shareSingleton].statu = @"unknown_internet";
                break;
                
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
                if (![[Singleton shareSingleton].statu isEqualToString:@"lost_internet"]) {
                    ViewController *errorVC = [[ViewController alloc] init];
                    [self.navigationController pushViewController:errorVC animated:NO];
                    [errorVC release];
                    [Singleton shareSingleton].statu = @"lost_internet";
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
                if (![[Singleton shareSingleton].statu isEqualToString:@"mobile_internet"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您现在是手机自带网络,建议切换到WIFI" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];
                    [Singleton shareSingleton].statu = @"mobile_internet";
                }
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
                [Singleton shareSingleton].statu = @"wifi_internet";
                break;
        }
    }];
    
    // 3.开始监控
    [mgr startMonitoring];
}

- (void)setLightDayModel {

}

- (void)setNightDayModel {

}

- (void)dealloc {
    
    // 必须在dealloc方法中移除观察
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    NSLog(@"Attention Memory Warning!!!!");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
